//
//  CalendarSignInHeaderView.m
//  CalendarDemo
//
//  Created by RYX on 2019/1/9.
//  Copyright © 2019 tuobu. All rights reserved.
//

#import "CalendarSignInHeaderView.h"

@interface CalendarSignInHeaderView ()

@property (nonatomic,weak) UILabel *desLa;
@property (nonatomic,weak) UIButton *signInBtn;

@property (nonatomic,assign) NSInteger hisValue;

@end

@implementation CalendarSignInHeaderView

- (instancetype)init{
    
    if (self = [super init]) {
        
        [self setUI];
    }
    return self;
}

- (void)setUI{
    
    self.backgroundColor = [UIColor colorWithRed:239/255.0 green:113/255.0 blue:108/255.0 alpha:1.0];
    
    UIButton *signInBtn = [[UIButton alloc] init];
    [signInBtn setImage:[UIImage imageNamed:@"签到"] forState:UIControlStateNormal];
    [signInBtn addTarget:self action:@selector(onSignInBtn:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:signInBtn];
    self.signInBtn = signInBtn;
    
    UILabel *desLa = [[UILabel alloc] init];
    NSMutableAttributedString *desString = [[NSMutableAttributedString alloc] initWithString:@"已连续签到0天，继续加油!" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Medium" size: 12],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]}];
    desLa.attributedText = desString;
    [self addSubview:desLa];
    self.desLa = desLa;
    
    [signInBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self);
        make.top.equalTo(self).offset(20);
        make.width.mas_equalTo(110);
        make.height.mas_equalTo(40);
        
    }];
    
    [desLa mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self);
        make.top.equalTo(signInBtn.mas_bottom).offset(20);
        
    }];

}


#pragma mark - 点击签到按钮
- (void)onSignInBtn:(UIButton *)sender{
    
    if ([sender.currentImage isEqual:[UIImage imageNamed:@"已签到"]]) {

        if (self.signInBlock != nil) {
            
            self.signInBlock(YES);
        }
        
    }else{
        
        if (self.signInBlock != nil) {
            
            self.signInBlock(NO);
        }
    }

}

- (UIViewController *)getCurrentViewController{
    
    for (UIView *next = [self superview]; next; next= next.superview) {
        
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UINavigationController class]] || [nextResponder isKindOfClass:[UIViewController class]]) {
            
            return (UIViewController*)nextResponder;
        }
    }
    return nil;
}

- (void)onBack:(UIButton *)sender{
    
    [[self getCurrentViewController].navigationController popViewControllerAnimated:YES];
}

- (void)setContinued:(NSString *)continued{
    
    self.hisValue = [continued integerValue];
    
    NSString *str = [NSString stringWithFormat:@"已连续签到%@天，继续加油!",continued];
    NSMutableAttributedString *desString = [[NSMutableAttributedString alloc] initWithString:str attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Medium" size: 12],NSForegroundColorAttributeName: [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0]}];
    self.desLa.attributedText = desString;
    
}

#pragma mark - 获取签到记录后刷新签到按钮
- (void)setHadSignIn:(BOOL)hadSignIn{
    
    [self.signInBtn setImage:[UIImage imageNamed:@"已签到"] forState:UIControlStateNormal];
    
}

@end
